package org.jeecg.modules.rms.util;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.util.RestUtil;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.UUID;

@Component
public class RMSTemplate {
    @Value("${rms.url}")
    private String url;
    @Value("${rms.username}")
    private String username;
    @Value("${rms.password}")
    private String password;

    /**
     * 搬运指定货架到目的地保持负载
     *
     * @param shelfCode    货架编码
     * @param destCellCode 目标单元格编码
     * @return
     */
    public JSONObject deliverShelf(String shelfCode, String destCellCode) {
        JSONObject request = new JSONObject();// 创建请求
        request.put("id", "wms_001");
        request.put("msgType", "RobotTaskRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();// 创建请求体
        body.put("taskType", "DELIVER_SHELF");
        body.put("shelfCode", shelfCode);
        body.put("instruction", "GO_RETURN");
        body.put("destCellCode", destCellCode);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post(url, request);// 发送请求
    }

    /**
     * 查询单个单元格信息
     *
     * @return
     */
    public JSONObject cellPoint(String cellCode) {
        JSONObject request = new JSONObject();// 创建请求
        request.put("id", "wms_001");
        request.put("msgType", "QueryInstructionRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();// 创建请求体
        body.put("instruction", "CELL");
        body.put("queryType", "detail");
        body.put("cellCode", cellCode);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post(url, request);// 发送请求
    }

    /**
     * 查询多个单元格信息
     * @param cellCodes 单元格编码集合
     * @return
     */
    public JSONObject cellPoints(List<String> cellCodes) {
        JSONObject request = new JSONObject();// 创建请求
        request.put("id", "wms_001");
        request.put("msgType", "QueryInstructionRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();// 创建请求体
        body.put("instruction", "CELL");
        body.put("queryType", "detail");
        body.put("cellCodes", cellCodes);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post(url, request);// 发送请求
    }

    //获取头信息
    @NotNull
    private JSONObject getHeaders() {
        JSONObject httpHeaders = new JSONObject();// 获取头信息
        httpHeaders.put("requestId", UUID.randomUUID());
        httpHeaders.put("clientCode", "wmsCode");
        httpHeaders.put("warehouseCode", "wmsWarehouseCode");
        httpHeaders.put("userId", username);
        httpHeaders.put("userKey", password);
        httpHeaders.put("language", "en_us");
        httpHeaders.put("version", "3.3.0");
        return httpHeaders;
    }

}
